package com.tgy.MyFlowable.workflow.controller;

import com.ctsi.core.common.api.Result;
import com.ctsi.core.common.util.$;
import com.ctsi.core.common.util.SecurityUtil;
import com.ctsi.core.web.controller.BaseController;
import com.ctsi.workflow.dto.FlwInstanceDTO;
import com.ctsi.workflow.service.IFlwInstanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 流程实例控制器
 *
 * @version 1.0
 * @author: wang xiao xiang
 * @date: 2021/8/20 9:44
 */
@RestController
@RequestMapping("/flow/instance")
@RequiredArgsConstructor
@Api(tags = "工作流：流程实例")
public class FlwInstanceController extends BaseController {

    private final IFlwInstanceService flwInstanceService;

    @GetMapping("/page")
    @ApiOperation(value = "分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", required = true, value = "当前页", paramType = "form"),
            @ApiImplicitParam(name = "size", required = true, value = "每页显示数据", paramType = "form"),
            @ApiImplicitParam(name = "keyword", required = true, value = "模糊查询关键词", paramType = "form"),
    })
    public Result<?> page(FlwInstanceDTO flwInstanceDTO) {
        return Result.data(flwInstanceService.page(flwInstanceDTO));
    }

    /**
     * 查询我的申请
     *
     * @param flwInstanceDTO 分页查询条件
     * @return 分页数据
     */
    @GetMapping("/my")
    @ApiOperation(value = "我的申请")
    public Result<?> my(HttpServletRequest request, FlwInstanceDTO flwInstanceDTO) {
        //flwInstanceDTO.setStartUserId($.toStr(MarsContextHolder.getUserId()));
        flwInstanceDTO.setStartUserId($.toStr(SecurityUtil.getUsername(request).getId()));
        return Result.data(flwInstanceService.page(flwInstanceDTO));
    }

    @GetMapping("/his-user-task")
    @ApiOperation(value = "历史用户任务")
    public Result<?> hisUserTask(FlwInstanceDTO flwInstanceDTO) {
        return Result.data(flwInstanceService.hisUserTask(flwInstanceDTO));
    }

    @GetMapping("/trace")
    @ApiOperation(value = "追踪", notes = "追踪")
    public Result<?> trace(FlwInstanceDTO flwInstanceDTO) {
        return Result.data(flwInstanceService.trace(flwInstanceDTO));
    }

    @PostMapping("/suspend")
    @ApiOperation(value = "挂起", notes = "挂起")
    public Result<?> suspend(@RequestBody FlwInstanceDTO flwInstanceDTO) {
        flwInstanceService.activeOrSuspend(flwInstanceDTO, Boolean.TRUE);
        return Result.condition(Boolean.TRUE);
    }

    @PostMapping("/active")
    @ApiOperation(value = "激活", notes = "激活")
    public Result<?> active(@RequestBody FlwInstanceDTO flwInstanceDTO) {
        flwInstanceService.activeOrSuspend(flwInstanceDTO, Boolean.FALSE);
        return Result.condition(Boolean.TRUE);
    }

//    @GetMapping("/comment-history")
//    @ApiOperation(value = "历史审批记录", notes = "历史审批记录")
//    public Result<?> commentHistory(FlwInstanceDTO flwInstanceDTO) {
//        return Result.data(flwInstanceService.commentHistory(flwInstanceDTO));
//    }

    @GetMapping("/hisUserTasks")
    @ApiOperation(value = "历史任务", notes = "历史任务")
    public Result<?> hisUserTasks(@RequestParam String id) {
        FlwInstanceDTO flwInstanceDTO = new FlwInstanceDTO();
        flwInstanceDTO.setId(id);
        return Result.data(flwInstanceService.hisUserTask(flwInstanceDTO));
    }

    @GetMapping("/comment-history")
    @ApiOperation(value = "历史审批记录", notes = "历史审批记录")
    public Result<?> commentHistory(@RequestParam String id) {
        FlwInstanceDTO flwInstanceDTO = new FlwInstanceDTO();
        flwInstanceDTO.setId(id);
        return Result.data(flwInstanceService.commentHistory(flwInstanceDTO));
    }

    @PostMapping("/end")
    @ApiOperation(value = "终止", notes = "终止")
    public Result<?> end(@RequestBody FlwInstanceDTO flwInstanceDTO) {
        flwInstanceService.end(flwInstanceDTO);
        return Result.condition(Boolean.TRUE);
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除", notes = "删除")
    public Result<?> del(@RequestBody List<FlwInstanceDTO> flwInstanceDTOList) {
        flwInstanceService.del(flwInstanceDTOList);
        return Result.condition(Boolean.TRUE);
    }

    @GetMapping("/form-data")
    @ApiOperation(value = "表单数据", notes = "表单数据")
    public Result<?> fromData(FlwInstanceDTO flwInstanceDTO) {
        return Result.data(flwInstanceService.formData(flwInstanceDTO));
    }

    @GetMapping("/add-sign-user-selector")
    @ApiOperation(value = "加签人员选择器", notes = "加签人员选择器")
    public Result<?> addSignUserSelector(FlwInstanceDTO flwInstanceDTO){
        return Result.data(flwInstanceService.addSignUserSelector(flwInstanceDTO));
    }

    @GetMapping("/del-sign-user-selector")
    @ApiOperation(value = "减签人员选择器", notes = "减签人员选择器")
    public Result<?> delSignUserSelector(FlwInstanceDTO flwInstanceDTO){
        return Result.data(flwInstanceService.delSignUserSelector(flwInstanceDTO));
    }
}
